package com.cfc.stage1;

import common.ListNode;
import org.junit.Test;

import java.math.BigDecimal;

/**
 * @description: 两数相加
 *   https://leetcode.cn/problems/add-two-numbers/description/
 * @author: yangxuan10
 * @create: 2024-04-11 09:08
 **/
public class AddTwoNumbers {

    @Test
    public void test1() {

        ListNode listNode = ListNode.arrayConversionListNode("2,4,3");
        ListNode listNode1 = ListNode.arrayConversionListNode("5,6,4");
        System.out.println(addTwoNumbers(listNode, listNode1));
    }

    /**
     * 官方题解，执行效率上和  addTwoNumbers2 一样的呢
     */
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = null, tail = null;
        int carry = 0;
        while (l1 != null || l2 != null) {
            int n1 = l1 != null ? l1.val : 0;
            int n2 = l2 != null ? l2.val : 0;
            int sum = n1 + n2 + carry;
            if (head == null) {
                head = tail = new ListNode(sum % 10);
            } else {
                tail.next = new ListNode(sum % 10);
                tail = tail.next;
            }
            carry = sum / 10;
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }
        if (carry > 0) {
            tail.next = new ListNode(carry);
        }
        return head;
    }

    public ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
        //两数相加进位的值
        int carryVal = 0;
        int l1IndexVal;
        int l2IndexVal;
        ListNode listNode = new ListNode();
        ListNode currentNode = listNode;
        while (l1 != null || l2 != null) {
            if (l1 != null) {
                l1IndexVal = l1.val;
                l1 = l1.next;
            } else {
                l1IndexVal= 0;
            }
            if (l2 != null) {
                l2IndexVal = l2.val;
                l2 = l2.next;
            } else {
                l2IndexVal =0;
            }
            int indexSum = l1IndexVal + l2IndexVal + carryVal;
            carryVal = indexSum / 10;
            currentNode.next = new ListNode(indexSum % 10);
            currentNode = currentNode.next;
        }
        if (carryVal == 1) {
            currentNode.next = new ListNode(carryVal);
            currentNode = currentNode.next;
        }
        return listNode.next;
    }

    /**
     * 第一种解法，但是leetcode平台不让用：BigDecimal
     */
    public ListNode addTwoNumbers1(ListNode l1, ListNode l2) {

        //将链表1输出成一个倒序的字符串啦
        //例如： 1 > 2 > 3  输出成 -> 321

        String l1Str = "";
        while (l1 != null) {
            String valueOf = "" + l1.val;
            valueOf += l1Str;
            l1Str = valueOf;
            l1 = l1.next;
        }
        String l2Str = "";
        while (l2 != null) {
            String valueOf = "" + l2.val;
            valueOf += l2Str;
            l2Str = valueOf;
            l2 = l2.next;
        }

        //将两个字符串的数组相加了，并且需要用BigDecimal，用int或者long会越界
        String finalStr = new BigDecimal(l1Str).add(new BigDecimal(l2Str)).toString();

        //然后倒序遍历存储到一个新的链表中呀
        ListNode newList = new ListNode(0);
        ListNode currentList = newList;
        char[] chars = finalStr.toCharArray();
        for (int i = chars.length - 1; i >= 0; i--) {
            String str = "" + chars[i];
            ListNode listNode = new ListNode(Integer.parseInt(str));
            currentList.next = listNode;
            currentList = listNode;
        }
        return newList.next;
    }

}